﻿using Newtonsoft.Json;
using ShareRss.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShareRss
{
    public class ConfigDll
    {
        /// <summary>
        /// 修改AppSettings中配置
        /// </summary>
        /// <param name="key">key值</param>
        /// <param name="value">相应值</param>
        public static bool SetConfigValue(string key, string value)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings[key] != null)
                    config.AppSettings.Settings[key].Value = value;
                else
                    config.AppSettings.Settings.Add(key, value);
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取AppSettings中某一节点值
        /// </summary>
        /// <param name="key"></param>
        public static string GetConfigValue(string key)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] != null)
                return config.AppSettings.Settings[key].Value;
            else

                return string.Empty;
        }

        /// <summary>
        /// 查找追番列表返回dt
        /// </summary>
        /// <returns></returns>
        public static DataTable ReadConfigToDT()
        {
            string loveList = ConfigDll.GetConfigValue("LoveList");
            if (!string.IsNullOrEmpty(loveList))
            {
                try
                {
                    var love = loveList.Split('@');
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Key");
                    dt.Columns.Add("Value");
                    for (int i = 0; i < love.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Key"] = $"{love[i].Split('_')[1]}  {PairSubtitleGroup(int.Parse(love[i].Split('_')[0]))}";
                        dr["Value"] = $"{int.Parse(love[i].Split('_')[0])}|{love[i].Split('_')[1]}";
                        dt.Rows.Add(dr);
                    }
                    return dt;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
            return null;
        }

        /// <summary>
        /// 通过ID查找字幕组名称
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string PairSubtitleGroup(int key)
        {
            string loveList = ConfigDll.GetConfigValue("Subtitle");
            var SubList = JsonConvert.DeserializeObject<List<KeyValueModel>>(loveList);
            var info = SubList.Where(x => x.Key == key).ToList();
            if (info.Count > 0)
            {
                return info[0].Value;
            }
            return string.Empty;
        }
    }
}
